package com.wx.mall.sys.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.sys.dto.OperateDto;
import com.wx.mall.sys.entity.OperateLog;
import com.wx.mall.sys.service.IOperateLogService;
import com.wx.mall.sys.vo.req.OperateDeleteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author j
 * @since 2020-04-27
 */
@RestController
@RequestMapping("/operate-log")
@Api(tags = "操作日志",description = "OperateLogController")
@Slf4j
public class OperateLogController {

   @Resource
   private IOperateLogService service;


    @ApiOperation("查询全部操作数据")
    @GetMapping("/list")
    public CommonResult<List<OperateLog>> ListAll(){


        List<OperateLog> list=service.list();

        return CommonResult.success(list);


    };

@ApiOperation("分页查询操作数据")
    @GetMapping("/getList")
    public CommonResult<CommonPage<OperateLog>> getList(Integer pageNum,
                                                        Integer pageSize){
    if (pageNum == null || pageSize == null) {
        ApiAssert.fail(ResultCode.VALIDATE_FAILED);
    }
    if (pageNum <= 0) {
        ApiAssert.fail("无效页码");
    };

    List<OperateLog> list=service.getList(pageNum,pageSize);

    return CommonResult.success(CommonPage.restPage(list));


};



    @ApiOperation("分页查询操作数据(筛选模式)")
    @GetMapping("/getListBySelect")
    public CommonResult<CommonPage<OperateLog>> getListBySelect(OperateDto dto) {
        System.out.println(dto);
        if (dto.getPageNum() == null || dto.getPageSize() == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() <= 0) {
            ApiAssert.fail("无效页码");
        };
//
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
//        Date time = simpleDateFormat.parse(operateTime);
//
//        System.out.println(time);
        List<OperateLog> list=service.getListBySelect(dto.getPageNum(),dto.getPageSize(),dto.getOperateName(),dto.getOperateTime());

        return CommonResult.success(CommonPage.restPage(list));


    };

    @ApiOperation("执行批量删除操作")
    @DeleteMapping("/deleteList")
    public CommonResult deleteList(@ApiParam("封装的删除数据vo")OperateDeleteVO vo){



        int sucess=service.deleteList(vo.getIds());

        return sucess==1?CommonResult.success():CommonResult.failed();

    };

    @ApiOperation("执行批量删除操作根据选择的时间")
    @DeleteMapping("/deleteListByType")
    public CommonResult deleteListByType(@ApiParam("封装的删除数据vo")OperateDeleteVO vo){

        System.out.println(vo);

        int sucess=service.deleteListByType(vo.getType());

        return sucess==1?CommonResult.success():CommonResult.failed();

    };

}

